package org.example.ymcc.consumer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.example.ymcc.constant.Constants;
import org.example.ymcc.constant.MQConstants;
import org.example.ymcc.constant.RConst;
import org.example.ymcc.domain.PayOrder;
import org.example.ymcc.mapper.PayOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "PayTimeOutConsumer",
        topic = MQConstants.ORDER_TIMEOUT_PAY_TOPIC,
        selectorExpression = MQConstants.ORDER_TIMEOUT_PAY_TAG
)
public class PayTimeOutConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private PayOrderMapper payOrderMapper;

    /**
     * 修改支付单状态
     * @param messageExt
     */
    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String sn = new String(body, Constants.UTF8);
            PayOrder payOrder = payOrderMapper.selectOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getOrderNo, sn));
            Integer payStatus = payOrder.getPayStatus();
            if(RConst.Num.INTEGER_0.equals(payStatus)){
                payOrder.setPayStatus(RConst.Num.INT_2);
                payOrderMapper.updateById(payOrder);
            }
            //调用支付宝的关单接口

        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
